package com.yune.activity.main.ui.me;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.yune.R;
import com.yune.activity.App;
import com.yune.activity.account.AccountActivity;
import com.yune.activity.download.DownloadActivity;
import com.yune.activity.download.DownloadViewModel;
import com.yune.activity.history.HistoryListActivity;
import com.yune.activity.login.LoginActivity;
import com.yune.activity.playlist.PlayListActivity;

import org.json.JSONException;
import org.json.JSONObject;
public class MeFragment extends Fragment {
    private TextView nickname,email;
    private View root;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (root==null) {
            root = inflater.inflate(R.layout.frgament_me, container, false);
            MeViewModel meViewModel = new ViewModelProvider(this).get(MeViewModel.class);
            nickname = root.findViewById(R.id.me_nickname);
            email = root.findViewById(R.id.me_email);
            root.findViewById(R.id.me_login).setOnClickListener(v -> requireActivity().startActivity(new Intent(requireContext(), AccountActivity.class)));
            root.findViewById(R.id.me_logout).setOnClickListener(v -> {
                requireActivity().startActivity(new Intent(requireActivity(), LoginActivity.class));
                SharedPreferences.Editor edit = App.sp.edit();
                edit.putBoolean("autoLogin", false);
                edit.apply();
                requireActivity().finish();
            });
            root.findViewById(R.id.me_playlist).setOnClickListener(v -> requireActivity().startActivity(new Intent(requireContext(), PlayListActivity.class)));
            root.findViewById(R.id.me_history).setOnClickListener(v -> requireActivity().startActivity(new Intent(requireContext(), HistoryListActivity.class)));
            root.findViewById(R.id.me_download).setOnClickListener(v -> requireActivity().startActivity(new Intent(requireContext(), DownloadActivity.class)));
            refresh();
        }
        return root;
    }
    private void refresh() {
        String loginMsg = App.sp.getString("loginMsg", "");
        try {
            JSONObject jsonObject=new JSONObject(loginMsg);
            nickname.setText(jsonObject.getString("nickname"));
            email.setText(jsonObject.getString("email"));
        } catch (JSONException e) {
            requireActivity().startActivity(new Intent(requireContext(), LoginActivity.class));
            Toast.makeText(requireActivity(), "登录失效，请重新登录", Toast.LENGTH_SHORT).show();
            requireActivity().finish();
        }
    }
    @Override
    public void onResume() {
        super.onResume();
        refresh();
    }
}
